import { Controller, Post, Request, UseGuards } from "@nestjs/common";
import { AuthService } from "./auth.service";
import { LocalAuthGuard } from "src/utils/guards/local-auth.guard";
import { Public } from "src/utils/decorators/public.decorator";
import { ApiBody, ApiOperation, ApiTags } from "@nestjs/swagger";
import { LoginFormDto } from "./dto/login-form-dto";

@Controller("auth")
@ApiTags("鉴权")
export class AuthController {
  constructor(private readonly authService: AuthService) {}

  // 登录方法
  // 在 Controller 和 Req 之间插入 Guard LocalAuthGuard 方法，验证账号和密码
  // 登录的Body被LocalAuthGuard拦截获取, login方法中不再需要
  @UseGuards(LocalAuthGuard)
  @Public()
  @Post("login")
  @ApiBody({
    description: "用户登录",
    type: LoginFormDto,
  })
  @ApiOperation({
    summary: "登录",
  })
  async login(@Request() req) {
    return this.authService.login(req.user);
  }
}
